package com.study.services;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.study.model.mdao.PushmsgMdao;
import com.study.model.pojo.Pushmsg;
@Service
public class PushmsgServices {
	
	@Autowired
	PushmsgMdao pmd;
	
	public void addPushmsg(Pushmsg m){
		m.setPid(this.getID());
		m.setSendtime(new Date());
		m.setState(0);
		pmd.addPushmsg(m);
	}
	
	/**
	 * 根据ID删除推送消息**/
	public void delPushmsg(String pid){
		pmd.delPushmsg(pid);
	};
	/**
	 * 查找员工的推送消息**/
	public List<Pushmsg> findByeid(int eid){
		return pmd.queryPSG(eid);
	}
	
	/**
	 * 自动生成ID*/
	public String getID(){
		DateFormat bf = new SimpleDateFormat("yyMMdd");
		String maxid=pmd.queryMaxid(bf.format(new Date()));
		String id="";
		if(maxid != null){
			int num=Integer.parseInt(maxid.substring(9,12));
			if((num+1)/100!=0){
				id=maxid.substring(0, 9)+(num+1);
			}else if((num+1)/10!=0){
				id=maxid.substring(0, 9)+"0"+(num+1);
			}else {
				id=maxid.substring(0, 9)+"00"+(num+1);
			}
		}else{
			id="msg"+bf.format(new Date())+"001";			
		}
		return id;
	}
}
